package asteroids.model;

import asteroids.CollisionListener;
import asteroids.Vector;

/**
 * @version	1.0
 * @author 	Jomi Verwimp
 */
public class Collision {
	
	/**
	 * @param 	obj1
	 * @param 	obj2
	 * @param 	ttc
	 * @param 	colPos
	 * 
	 * @post	| new.getObject1() == obj1
	 * @post	| new.getObject2() == obj2
	 * @post	| new.getTimeToCollision() == ttc
	 * @post	| new.getCollisionPosition() == colPos
	 */
	public Collision(GameObject obj1, WorldObject obj2, double ttc, Vector colPos) {
		this.obj1 = obj1;
		this.obj2 = obj2;
		this.timeToCollision = ttc;
		this.collisionPosition = colPos;
	}
	
	/**
	 * @return	| result == obj1
	 */
	public GameObject getObject1() {
		return obj1;
	}
	
	private final GameObject obj1;
	
	/**
	 * @return	| result == obj2
	 */
	public WorldObject getObject2() {
		return obj2;
	}
	
	private final WorldObject obj2;
	
	/**
	 * @return	| result == timeToCollision
	 */
	public double getTimeToCollision() {
		return this.timeToCollision;
	}
	
	private final double timeToCollision;
	
	/**
	 * @return	| result == collisionPosition
	 */
	public Vector getCollisionPosition() {
		return this.collisionPosition;
	}
	
	private final Vector collisionPosition;
	
	/**
	 * @param 	cl
	 * 
	 * @effect | if(gObj2 instanceof HorBound || gObj2 instanceof VerBound)
	 * 		   |	then cl.boundaryCollision(gObj1,getCollisionPosition().getX(),getCollisionPosition().getY())
	 * 		   | else cl.objectCollision(gObj1, gObj2, getCollisionPosition().getX(), getCollisionPosition().getY())
	 * 		   | gObj2.collide(gObj1)
	 */
	public void resolve(CollisionListener cl) {
		GameObject gObj1 = getObject1();
		WorldObject gObj2 = getObject2();
		if(gObj2 instanceof HorBound || gObj2 instanceof VerBound) {
			cl.boundaryCollision(gObj1, getCollisionPosition().getX(), getCollisionPosition().getY());
		} else {
			cl.objectCollision(gObj1, gObj2, getCollisionPosition().getX(), getCollisionPosition().getY());
		}
		gObj2.collide(gObj1);
	}

}
